package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.service.MemberSrevice;
import com.atguigu.service.SetmealService;
import com.atguigu.util.DateUtils;
import com.atguigu.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Report")
public class ReportController {

    @Reference
    MemberSrevice memberSrevice;

    @Reference
    SetmealService setmealService;




    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map map = new HashMap();

            List<String> months = new ArrayList<>();


            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-12);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            for (int i = 0; i<12;i++){
                calendar.add(Calendar.MONTH,1);
                months.add(sdf.format(calendar.getTime()));

            }

            List<Integer> memberCount= memberSrevice.findMemberCountByMonths(months);


            map.put("months",months);
            map.put("memberCount",memberCount);

            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map map = new HashMap();

            List<String> setmealNames = new ArrayList<>();

            List<Map<String,Object>> setmealCount = setmealService.getSetmealReport();

            for (Map<String, Object> mapDate : setmealCount) {
                String name = (String) mapDate.get("name");
                setmealNames.add(name);
            }

            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);


            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);

        }
    }

//    @GetMapping("/getBusinessReportData")
//    public Result getBusinessReportData(){
//
//        try {
//            List<Map<String,Object>> reportData = new ArrayList<>();
//
//            Map<String,Object> map = new HashMap<>();
//
//            map.put("reportDate", DateUtils.parseDate2String(new Date()));
//
//
//
//            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,reportData);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_FAIL);
//        }
//    }
}
